'''
Created on Nov 6, 2009

@author: kage
'''

from PyQt4 import QtCore, QtGui

from PyBatch.uiFiles.options import Ui_Options

class Settings(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, None)
        self.parent = parent
        self.ui = Ui_Options()
        self.ui.setupUi(self)
        self.initialiseParams()
        self.initialiseGuiElements()
        self.tieupLooseEnds()
    
    def slotUpdate(self):
         self.parent.appConfig['inputFolder'] = self.__inputPath
         self.parent.appConfig['outputFolder'] = self.__outputPath
         self.parent.appConfig['priority'] = self.__priority
         self.parent.appConfig['title'] = self.__title
         self.close()
    
    def showEvent(self, event):
        self.initialiseParams()
        self.initialiseGuiElements()
        event.accept()
    
    def initialiseParams(self):
        self.__inputPath = self.parent.appConfig['inputFolder']
        self.__outputPath = self.parent.appConfig['outputFolder']
        self.__priority = self.parent.appConfig['priority']
        self.__title = self.parent.appConfig['title']
        
    def initialiseGuiElements(self):
        self.ui.defaultInputPath.setText(self.__inputPath)
        self.ui.defaultOutputPath.setText(self.__outputPath)
        self.ui.title.setText(self.__title)
        self.ui.priority.setCurrentIndex(self.ui.priority.findText(self.__priority))
        
    def tieupLooseEnds(self):
        con = QtCore.QObject.connect
        
        con(self.ui.browseInput, QtCore.SIGNAL("clicked()"), self.__slotDefaultInput)
        con(self.ui.browseOutput, QtCore.SIGNAL("clicked()"), self.__slotDefaultOutput)
        con(self.ui.title, QtCore.SIGNAL("textChanged(QString)"), self.__slotUpdateTitle)
        con(self.ui.priority, QtCore.SIGNAL("currentIndexChanged(QString)"), self.__slotUpdatePriority)
        con(self.ui.okButton, QtCore.SIGNAL("clicked()"), self.slotUpdate)
        con(self.ui.cancelButton, QtCore.SIGNAL("clicked()"), self.close)
        
    def __slotDefaultInput(self):
        folder = QtGui.QFileDialog.getExistingDirectory(self, 'Select default input folder', self.__inputPath, QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks)
        if(folder != ''):
            self.ui.defaultInputPath.setText(folder)
            self.__inputPath = str(folder)
            
    def __slotDefaultOutput(self):
        folder = QtGui.QFileDialog.getExistingDirectory(self, 'Select default output folder', self.__outputPath, QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks)
        if(folder != ''):
            self.ui.defaultOutputPath.setText(folder)
            self.__outputPath = str(folder)
    
    def __slotUpdateTitle(self, data):
        self.__title = str(data)
        
    def __slotUpdatePriority(self, data):
        self.__priority = str(data)
    
